package edu.ptu.communityplatform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import edu.ptu.communityplatform.common.controller.AjaxResponse;
import edu.ptu.communityplatform.common.controller.BaseController;
import edu.ptu.communityplatform.common.controller.TableInfo;
import edu.ptu.communityplatform.domain.entity.Car;
import edu.ptu.communityplatform.domain.entity.PlotCar;
import edu.ptu.communityplatform.domain.queryParam.carParam.CarAddParam;
import edu.ptu.communityplatform.domain.queryParam.carParam.CarUpdateParam;
import edu.ptu.communityplatform.domain.vos.CarPageVo;
import edu.ptu.communityplatform.domain.vos.PersonInfoVo;
import edu.ptu.communityplatform.service.ICarService;
import edu.ptu.communityplatform.service.IPersonService;
import edu.ptu.communityplatform.service.IPlotCarService;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;


/**
 * @BelongsProject: communityPlatform
 * @BelongsPackage: edu.ptu.communityplatform.controller
 * @Author: HYJ
 * @CreateTime: 2025-04-06  22:00
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@RequestMapping("/car")
public class CarController extends BaseController {

    @Resource
    private ICarService carService;

    @Resource
    private IPersonService personService;

    @Resource
    private IPlotCarService plotCarService;

    /**
     * 添加车辆信息
     *
     * @param param 车辆信息
     * @return 响应结果
     */
    @Transactional
    @PostMapping()
    public AjaxResponse addCar(@RequestBody CarAddParam param) {
        // 从请求体中获取车牌号和用户ID
        String carNumber = param.getCarNumber();
        Long personId = param.getPersonId();

        if (carNumber == null || Objects.equals(carNumber, "")) {
            return AjaxResponse.fail("车牌号不能为空");
        }

        if (personId == null) {
            return AjaxResponse.fail("用户ID不能为空");
        }

        // 构建车辆对象并设置属性
        Car car = new Car();
        car.setCarNumber(carNumber);
        car.setPersonId(personId);

        // 调用服务层保存车辆信息
        boolean save = carService.save(car);


        //添加到plot_car表中
        Long plotId = personService.getPlotIdByPersonId(personId);
        Long carId = car.getCarId();

        PlotCar plotCar = new PlotCar();
        plotCar.setPlotId(plotId);
        plotCar.setCarId(carId);

        boolean plotCarSaved = plotCarService.save(plotCar);

        //根据保存结果返回响应
        return save ? AjaxResponse.success("success") : AjaxResponse.fail("fail");

    }

    /**
     * 根据车辆ID查询车辆信息
     *
     * @param carId 车辆ID
     * @return 车辆信息
     */
    @GetMapping("/dtl/{carId}")
    public AjaxResponse getCarById(@PathVariable Long carId) {
        Car car = carService.getById(carId);
        return car != null ? AjaxResponse.success(car) : AjaxResponse.fail("fail");
    }

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @param plotId
     * @param carNumber
     * @return
     */
    @GetMapping("/list")
    public TableInfo getCarList(
            @RequestParam(required = true, defaultValue = "1") Integer pageNum,
            @RequestParam(required = true, defaultValue = "5") Integer pageSize,
            @RequestParam(required = true) Long plotId,
            @RequestParam(required = false) String carNumber
    ) {
        IPage<CarPageVo> carPage = carService.getCarPage(pageNum, pageSize, plotId, carNumber);

        // 获取车辆信息列表
        List<CarPageVo> carPageVos = carPage.getRecords();
        return toJson(carPage != null, carPageVos, carPageVos.size());
    }

    /**
     * 修改车辆信息
     *
     * @param param 车辆信息
     * @return 响应结果
     */
    @Transactional
    @PutMapping()
    public AjaxResponse updateCar(@RequestBody CarUpdateParam param) {
        Long carId = param.getCarId();
        String carNumber = param.getCarNumber();
        Long personId = param.getPersonId();

        //校验参数
        if (carNumber == null || Objects.equals(carNumber, "")) {
            return AjaxResponse.fail("车牌号不能为空");
        }

        //车辆表wrapper
        LambdaQueryWrapper<Car> carWrapper = new LambdaQueryWrapper<>();
        carWrapper.eq(Car::getCarId, carId);

        //修改车辆表
        Car car = new Car();
        car.setPersonId(personId);
        car.setCarNumber(carNumber);

        boolean updateCar = carService.update(car, carWrapper);

        //修改plot_car表
        Long plotId = personService.getPlotIdByPersonId(personId);

        LambdaQueryWrapper<PlotCar> plotCarWrapper = new LambdaQueryWrapper<>();
        plotCarWrapper.eq(PlotCar::getCarId, carId);

        //修改plot_car表
        PlotCar plotCar = new PlotCar();
        plotCar.setPlotId(plotId);

        boolean updatePlotCar = plotCarService.update(plotCar, plotCarWrapper);
        // 根据更新结果返回响应
        return updateCar && updatePlotCar ? AjaxResponse.success("success") : AjaxResponse.fail("fail");
    }


    /**
     * 分页查询人员表
     *
     * @param pageNum
     * @param pageSize
     * @param plotId
     * @param personName
     * @return
     */
    @GetMapping("/getPersonInfo")
    public TableInfo getPersonInfo(
            @RequestParam(required = true, defaultValue = "1") Integer pageNum,
            @RequestParam(required = true, defaultValue = "5") Integer pageSize,
            @RequestParam(required = true) Long plotId,
            @RequestParam(required = false) String personName
    ) {
        // 调用服务层获取人员信息列表
        IPage<PersonInfoVo> infoPage = personService.getPersonInfoPage(pageNum, pageSize, plotId, personName);

        // 人员详细表格信息
        List<PersonInfoVo> personInfoVos = infoPage.getRecords();
        return toJson(infoPage != null, personInfoVos, personInfoVos.size());
    }

    @Transactional
    @DeleteMapping("/{carId}")
    public AjaxResponse deleteCar(@PathVariable Long carId) {
        //1.删car表
        boolean carDeleted = carService.removeById(carId);

        // 2.删plot_car表
        LambdaQueryWrapper<PlotCar> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PlotCar::getCarId, carId);

        boolean plotCarDeleted = plotCarService.remove(wrapper);
        return carDeleted && plotCarDeleted ? AjaxResponse.success("success") : AjaxResponse.fail("fail");
    }
}
